package com.csii.activiti.server.core.variable.discover.impl;

import com.csii.activiti.server.core.constant.CommonConstant;
import com.csii.activiti.server.core.constant.FlowNodeVariableTypeConstant;
import com.csii.activiti.server.core.entity.ActFlowNode;
import com.csii.activiti.server.core.variable.discover.ActVariableProcessorDiscover;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class ActFlowOptionalVariableProcessor implements ActVariableProcessorDiscover {

    @Autowired
    private RuntimeService runtimeService;

    @Override
    public String getVariableName() {
        return "发起人自选";
    }

    @Override
    public String getVariableType() {
        return FlowNodeVariableTypeConstant.OPTIONAL;
    }

    @Override
    public boolean needManyPeopleReview() {
        return true;
    }

    @Override
    public boolean needEmptyProcessing() {
        return false;
    }

    @Override
    public int order() {
        return 1;
    }

    @Override
    public Set<String> getUsernames(ActFlowNode actFlowNode, String executionId, ExecutionEntityImpl processInstance) {
        Map variable = runtimeService.getVariable(executionId, CommonConstant.FORM_DATA, Map.class);
        List<String> optionalUsers = (List<String>) variable.get(actFlowNode.getVariableValue());
        if (optionalUsers != null){
            return new HashSet<>(optionalUsers);
        }
        return null;
    }
}
